$('.table').bootstrapTable({
    classes: '',
    locale: 'zh-CN',
    uniqueId: "id",
    idField: "id",
    theadClasses: 'text-primary',
    url: urlPre + '/page',
    pagination: true,
    sidePagination: "server",
    columns: columns,
    responseHandler: function (response) {
        return response.data;
    }
});

function save() {
    $.ajax({
        url: urlPre,
        type: 'POST',
        data: $('form').serialize(),
        success: response => refresh()
    })
}

function remove() {
    let ids = getSelectedIds();
    ids.forEach(id => {
        $.ajax({
            url: urlPre + '/' + id,
            type: 'DELETE',
            success: response => refresh()
        })
    })
}

function getSelectedIds() {
    var array = $(".table").bootstrapTable("getSelections");
    if (!array || array.length == 0) {
        showNotification('top', 'right')
        return null;
    }
    var ids = new Array();
    if (array.length == 0) {
        return null;
    }
    for (var i = 0; i < array.length; i++) {
        ids[i] = array[i].id;
    }
    return ids;
}

function refresh() {
    $('.table').bootstrapTable('refresh', {url: urlPre + '/page'});
}

function view() {
    $('input').each((i, ele) => {
        $(ele).val('');
    });
    $('#editModal').modal()
}

function review() {
    let ids = getSelectedIds();
    if (ids == null) {
        return;
    }
    $('input').each((i, ele) => {
        $(ele).val('');
    });
    let id = ids[0];
    $.ajax({
        url: urlPre + '/' + id,
        type: 'GET',
        success: function (response) {
            $('input').each((i, ele) => {
                var attribute = ele.getAttribute('name');
                if (attribute && response.data[attribute] != null) {
                    $(ele).val(response.data[attribute]);
                }
            });
            $('#editModal').modal()
        }
    })
}

function getSelectedIds() {
    var array = $(".table").bootstrapTable("getSelections");
    if (!array || array.length == 0) {
        showNotification('top', 'right')
        return null;
    }
    var ids = new Array();
    if (array.length == 0) {
        return null;
    }
    for (var i = 0; i < array.length; i++) {
        ids[i] = array[i].id;
    }
    return ids;
}

function showNotification(from, align) {

    $.notify({
        icon: "add_alert",
        message: "请先选择列表中的数据。"

    }, {
        type: 'warning',
        timer: 2,
        placement: {
            from: from,
            align: align
        }
    });
}

function initUserSelect() {
    $.ajax({
        url: '/user/checkItem',
        type: 'GET',
        success: function (response) {
            response.data.forEach(user => {
                $('#userSelect').append('<a class="dropdown-item" href="#" data-attribute="' + user.id + '" onclick="changeVal(this,\'#user\')">' + user.name + '</a>')
            })
        }
    })
}

function initGoodsSelect() {
    $.ajax({
        url: '/goods/checkItem',
        type: 'GET',
        success: function (response) {
            response.data.forEach(goods => {
                $('#goodsSelect').append('<a class="dropdown-item" href="#" data-attribute="' + goods.id + '" onclick="changeVal(this,\'#goods\')">' + goods.name + '</a>')
            })
        }
    })
}
